package com.aigou.admin.aigouspringboot.controller.admin;


import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.ReceivingAddress;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.ReceivingAddressService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/admin/receivingaddress")
@Slf4j
public class ReceivingAddressController {
    @Resource
    private ReceivingAddressService receivingAddressService;

    @GetMapping
    @LogScope(title = "收货地址管理模块", content = "查看收货地址列表信息")
    public Result select(@RequestParam Integer current) {

        log.info("查询第{}页的订单信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<ReceivingAddress> receivingAddressPageInfo = receivingAddressService.selectReceivingAddressListByPage(current);
        return Result.success(receivingAddressPageInfo);
    }


    @GetMapping("/{id}")
    @LogScope(title = "收货地址管理模块", content = "根据id查询收货地址信息")
    public Result detail(@PathVariable String id){
        ReceivingAddress receivingaddress = receivingAddressService.selectById(id);
        return Result.success(receivingaddress);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "收货地址管理模块", content = "根据id删除收货地址信息")
    public Result delete(@PathVariable String id ,@RequestHeader("Authorization") String token){
        int i = receivingAddressService.deleteById(id,token);
        return i>0 ? Result.success() : Result.error();
    }



    @PutMapping
    @LogScope(title = "收货地址管理模块", content = "编辑收货地址信息")
    public Result edit(@RequestBody ReceivingAddress receivingaddress){

        int i = receivingAddressService.edit(receivingaddress);
        return i>0 ? Result.success() : Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "收货地址管理模块", content = "添加用户收货地址信息")
    public Result save(@RequestBody ReceivingAddress receivingaddress){

        receivingaddress.setId(RandomUtil.getRandomNumber());
        int i = receivingAddressService.save(receivingaddress);
        return i>0 ? Result.success() : Result.error();
    }

}
